import { SpuVO } from "@/api/product/spu/type";
import { PageInstanceVO } from "@/api/type";

let spuList: SpuVO[] = [
  {
    id: 1,
    spuName: '小米',
    description: '小米',
    category3Id: 12,
    tmId: 1,
    spuSaleAttrList: [
      {
        id: 1,
        spuId: 1,
        baseSaleAttrId: 1,
        saleAttrName: '颜色',
        spuSaleAttrValueList: [
          {
            id: 1,
            spuId: 1,
            baseSaleAttrId: 1,
            saleAttrValueName: '红色',
          },
          {
            id: 2,
            spuId: 1,
            baseSaleAttrId: 1,
            saleAttrValueName: '蓝色',
          },
          {
            id: 3,
            spuId: 1,
            baseSaleAttrId: 1,
            saleAttrValueName: '黑色',
          }
        ]

      }],
    spuImageList: [
      {
        id: 1,
        spuId: 1,
        imgName: '小米',
        imgUrl: 'https://oss.aliyuncs.com/aliyun_cdn/aliyun-cdn-oss-cdn-2016/2016-09-30/2016093009000096.jpg',
        productImgId: 1,
        isDefault: false
      },
      {
        id: 2,
        spuId: 1,
        imgName: '小米',
        imgUrl: 'https://oss.aliyuncs.com/aliyun_cdn/aliyun-cdn-oss-cdn-2016/2016-09-30/2016093009000096.jpg',
        productImgId: 1,
        isDefault: false
      }
    ]
  },
  {
    id: 2,
    spuName: '小米9',
    description: '小米9',
    category3Id: 61,
    tmId: 1,
    spuSaleAttrList: [
      {
        id: 1,
        spuId: 2,
        baseSaleAttrId: 1,
        saleAttrName: '颜色',
        spuSaleAttrValueList: []
      }
    ],
    spuImageList: [
      {
        id: 1,
        spuId: 2,
        imgName: '小米',
        imgUrl: 'https://oss.aliyuncs.com/aliyun_cdn/aliyun-cdn-oss-cdn-2016/2016-09-30/2016093009000096.jpg',
        productImgId: 1,
        isDefault: false
      },
      {
        id: 2,
        spuId: 2,
        imgName: '小米',
        imgUrl: 'https://oss.aliyuncs.com/aliyun_cdn/aliyun-cdn-oss-cdn-2016/2016-09-30/2016093009000096.jpg',
        productImgId: 1,
        isDefault: false
      }
    ]
  },
  // 换成华为
  {
    id: 2,
    spuName: '华为',
    description: '华为手机',
    category3Id: 12,
    tmId: 1,
    spuSaleAttrList: [
      {
        id: 1,
        spuId: 2,
        baseSaleAttrId: 1,
        saleAttrName: '颜色',
        spuSaleAttrValueList: [
          {
            id: 1,
            spuId: 2,
            baseSaleAttrId: 1,
            saleAttrValueName: '红色',
          },
          {
            id: 2,
            spuId: 2,
            baseSaleAttrId: 1,
            saleAttrValueName: '蓝色',
          },
          {
            id: 3,
            spuId: 2,
            baseSaleAttrId: 1,
            saleAttrValueName: '黑色',
          }
        ]
      }
    ],
    spuImageList: [
      {
        id: 1,
        spuId: 2,
        imgName: '1.jpg',
        imgUrl: 'https://picsum.photos/200/300',
        productImgId: 1,
        isDefault: false
      },
      {
        id: 2,
        spuId: 2,
        imgName: '2.jpg',
      }
    ]
  },
  // 苹果的
  {
    id: 3,
    spuName: 'iPhone 14 Pro',
    description: 'iPhone 14 Pro',
    category3Id: 12,
    tmId: 1,
    spuSaleAttrList: [
      {
        id: 1,
        spuId: 2,
        baseSaleAttrId: 1,
        saleAttrName: '颜色',
        spuSaleAttrValueList: [
          {
            id: 1,
            spuId: 2,
            baseSaleAttrId: 1,
            saleAttrValueName: '红色',
          },
          {
            id: 2,
            spuId: 2,
            baseSaleAttrId: 1,
            saleAttrValueName: '蓝色',
          },
        ]
      }
    ],
    spuImageList: [
      {
        id: 1,
        spuId: 2,
        imgName: '1.jpg',
        imgUrl: 'https://picsum.photos/200/300',
        productImgId: 1,
        isDefault: false
      },
      {
        id: 2,
        spuId: 2,
        imgName: '2.jpg',
      }
    ]
  },


]


function getSpuList(page: number, limit: number, category3Id: number): PageInstanceVO<SpuVO> {
  let filteredList = spuList.filter(item => item.category3Id === category3Id);
  return {
    list: filteredList.slice((page - 1) * limit, page * limit),
    total: filteredList.length,
    size: limit,
    current: page,
    searchCount: false,
    pages: Math.ceil(filteredList.length / limit)
  }

}


export default [
  {
    url: '/api/admin/product/spu/list',
    method: 'get',
    response: (
      { query }: {
        query: { page: number, limit: number, category3Id: number }
      }
    ) => {
      console.log('query:', query);
      const page = Number(query.page)
      const limit = Number(query.limit)
      const category3Id = Number(query.category3Id);
      let data = getSpuList(page, limit, category3Id);
      return {
        code: 200,
        data: data,
        message: 'success',
      }
    }
  },
  {
    url: '/api/admin/product/spu/delete/:supId',
    method: 'delete',
    response: ({ query }: { query: { supId: number } }) => {
      const supId = Number(query.supId);
      // 删除 列表中id 为 supId的数据
      const index = spuList.findIndex((item) => item.id === supId);
      if (index !== -1) {
        spuList.splice(index, 1);
      }
      return {
        code: 200,
        data: null,
        message: 'success'
      }

    }
  }

]